<template>
  <div>
    <app-header />
    <div class="container page-container">
      <div class="blog-detail" v-if="post">
        <div class="card blog-content">
          <h1 class="blog-title">{{ post.title }}</h1>
          <div class="blog-meta">
            <span>作者: {{ post.author }}</span>
            <span class="meta-divider">|</span>
            <span>{{ formatDate(post.createdAt) }}</span>
            <span class="meta-divider">|</span>
            <span>分类: {{ post.category }}</span>
          </div>
          <div class="blog-body" v-html="post.content"></div>
        </div>
        
        <div class="card comments-section">
          <h3>评论 ({{ post.comments ? post.comments.length : 0 }})</h3>
          <div v-if="post.comments && post.comments.length > 0">
            <div v-for="(comment, index) in post.comments" :key="index" class="comment">
              <div class="comment-header">
                <strong>{{ comment.name }}</strong>
                <span class="comment-date">{{ formatDate(comment.date) }}</span>
              </div>
              <div class="comment-content">{{ comment.content }}</div>
            </div>
          </div>
          <div v-else class="no-comments">
            暂无评论，来添加第一条评论吧!
          </div>
          
          <div class="comment-form">
            <h4>发表评论</h4>
            <el-form :model="commentForm" @submit.prevent="submitComment">
              <el-form-item>
                <el-input v-model="commentForm.name" placeholder="您的名字" />
              </el-form-item>
              <el-form-item>
                <el-input 
                  v-model="commentForm.content" 
                  type="textarea" 
                  rows="4"
                  placeholder="输入您的评论"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitComment">提交评论</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      
      <div v-else-if="loading" class="loading-container">
        <el-skeleton :rows="10" animated />
      </div>
      
      <div v-else class="not-found card">
        <el-empty description="未找到该博客文章" />
        <div class="back-btn-container">
          <el-button @click="$router.push('/')">返回首页</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import AppHeader from '../components/app-header.vue'

const route = useRoute()
const router = useRouter()
const post = ref(null)
const loading = ref(true)
const commentForm = ref({
  name: '',
  content: ''
})

const fetchPost = async () => {
  loading.value = true
  const postId = route.params.id
  
  try {
    const response = await axios.get(`/api/posts/${postId}`)
    post.value = response.data
  } catch (error) {
    console.error('Failed to fetch post details:', error)
  } finally {
    loading.value = false
  }
}

const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

const submitComment = async () => {
  if (!commentForm.value.name || !commentForm.value.content) {
    ElMessage.warning('请填写名字和评论内容')
    return
  }
  
  try {
    await axios.post(`/api/posts/${route.params.id}/comments`, {
      name: commentForm.value.name,
      content: commentForm.value.content,
      date: new Date().toISOString()
    })
    
    ElMessage.success('评论提交成功')
    
    // Clear form and refresh comments
    commentForm.value.name = ''
    commentForm.value.content = ''
    fetchPost()
  } catch (error) {
    console.error('Failed to submit comment:', error)
    ElMessage.error('评论提交失败')
  }
}

onMounted(() => {
  fetchPost()
})
</script>

<style lang="less" scoped>
.blog-detail {
  max-width: 800px;
  margin: 0 auto;
}

.blog-title {
  margin-top: 0;
  margin-bottom: 15px;
  font-size: 28px;
  color: #303133;
}

.blog-meta {
  color: #909399;
  font-size: 14px;
  margin-bottom: 25px;
  
  .meta-divider {
    margin: 0 8px;
  }
}

.blog-body {
  font-size: 16px;
  line-height: 1.8;
  color: #303133;
  
  p {
    margin-bottom: 20px;
  }
  
  img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    margin: 20px 0;
  }
  
  h2, h3 {
    margin-top: 30px;
    margin-bottom: 15px;
  }
  
  ul, ol {
    margin-bottom: 20px;
    padding-left: 20px;
    
    li {
      margin-bottom: 8px;
    }
  }
  
  blockquote {
    border-left: 4px solid #409eff;
    padding: 10px 20px;
    background-color: #f8f9fb;
    margin: 20px 0;
    font-style: italic;
  }
  
  pre {
    background-color: #282c34;
    color: #abb2bf;
    padding: 15px;
    border-radius: 5px;
    overflow: auto;
    margin: 20px 0;
  }
}

.comments-section {
  margin-top: 30px;
  
  h3 {
    margin-top: 0;
    margin-bottom: 20px;
    font-size: 20px;
  }
}

.comment {
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
  
  &:last-child {
    border-bottom: none;
  }
  
  .comment-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    
    .comment-date {
      color: #909399;
      font-size: 12px;
    }
  }
  
  .comment-content {
    color: #606266;
    line-height: 1.6;
  }
}

.no-comments {
  color: #909399;
  text-align: center;
  padding: 20px 0;
}

.comment-form {
  margin-top: 30px;
  
  h4 {
    margin-top: 0;
    margin-bottom: 15px;
  }
}

.loading-container {
  max-width: 800px;
  margin: 0 auto;
}

.not-found {
  max-width: 800px;
  margin: 0 auto;
  text-align: center;
  padding: 40px;
  
  .back-btn-container {
    margin-top: 20px;
  }
}
</style>

